﻿using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using NetMessage;

namespace CreateNetMessage
{
    /// <summary>
    /// 消息管理
    /// </summary>
    internal class MessageManager
    {
        /// <summary>
        /// 当前编辑器里存在的消息
        /// </summary>
        private List<Message> messages = new List<Message>();

        private static string SaveFile
        {
            get { return "Message.xml"; }
        }

        /// <summary>
        /// 当前编辑器里存在的消息
        /// </summary>
        public List<Message> Messages
        {
            get { return messages; }
        }

        /// <summary>
        /// 从文件里加载消息
        /// </summary>
        public void Load()
        {
            if (File.Exists(SaveFile))
            {
                var ser = new XmlSerializer(typeof (List<Message>));
                using (var fs = new FileStream(SaveFile, FileMode.Open))
                {
                    messages = ser.Deserialize(fs) as List<Message>;
                    fs.Close();
                }
            }
        }

        /// <summary>
        /// 保存当前的消息
        /// </summary>
        public void Save()
        {
            var ser = new XmlSerializer(typeof (List<Message>));
            using (var fs = new FileStream(SaveFile, FileMode.Create))
            {
                ser.Serialize(fs, messages);
                fs.Close();
            }
        }
    }
}